package cn.tedu.ximart.server.controller;


import cn.tedu.ximart.server.pojo.dto.AttributeTemplateAddNewDTO;
import cn.tedu.ximart.server.pojo.vo.AttributeTemplateListItemVO;
import cn.tedu.ximart.server.service.IAttributeTemplateService;
import cn.tedu.ximart.server.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Api(tags = "属性模板管理模块")
@RestController
@RequestMapping("/attribute-templates")
public class AttributeTemplateController {

    @Autowired
    private IAttributeTemplateService service;

    @ApiOperation("添加属性模板")
    @ApiOperationSupport(order = 100)
    @PostMapping("/add-new")
    @PreAuthorize("hasAnyAuthority('/pms/product/delete','/pms/product/read','/pms/product/update')")
    public JsonResult addNew(@RequestBody AttributeTemplateAddNewDTO attributeTemplateAddNewDTO){
        log.debug("接收到的请求参数：{}",attributeTemplateAddNewDTO);
        service.addNew(attributeTemplateAddNewDTO);
        return JsonResult.ok();
    }


    @ApiOperation("删除属性模板")
    @ApiOperationSupport(order = 200)
    @PostMapping("/{id:[0-9]+}/delete")
    @PreAuthorize("hasAnyAuthority('/pms/product/delete','/pms/product/read','/pms/product/update')")
    public JsonResult deleteById(@PathVariable Long id){
        log.debug("接收到的请求参数：{}",id);
        service.deleteById(id);
        return JsonResult.ok();
    }

    @ApiOperation("查看属性模板列表")
    @ApiOperationSupport(order = 200)
    @GetMapping("")
    @PreAuthorize("hasAnyAuthority('/pms/product/delete','/pms/product/read','/pms/product/update')")
    public JsonResult list(){
        List<AttributeTemplateListItemVO> list = service.list();
        return JsonResult.ok(list);
    }



}
